using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ProjectSurvivor;
using QFramework;
using UnityEngine;
using UnityEngine.UI;

public class ExpUpgrdeSystem : AbstractSystem, ICanSave//定义经验升级模块
{
    public static EasyEvent OnExpUpgrdeSystemChanged = new EasyEvent();//EasyEvent 用于无参事件（如系统变动事件）。
    public List<UIGamePlanelItem> Items { get; } = new List<UIGamePlanelItem>();
    public List<Button> TypeItems { get; set; } = new List<Button>();
    public Dictionary<string, UIGamePlanelItem> Dictionary = new Dictionary<string, UIGamePlanelItem>();
    public Dictionary<string, string> Pairs = new Dictionary<string, string>()
    {
        { "simple_sword", "simple_critical" },
        {"simple_bomb", "simple_fly_count" },
        {"simple_knife", "damage_rate" },
        {"basket_ball", "movement_speed_rate" },
        {"rotate_sword", "simple_exp" },
        { "simple_critical", "simple_sword" },
        {"simple_fly_count", "simple_bomb" },
        {"damage_rate", "simple_knife" },
        {"movement_speed_rate","basket_ball" },
        {"simple_exp", "rotate sword" }

    };
    public Dictionary<string, BindableProperty<bool>> Pairedproperties =
    new()
    {
            { "simple_sword", Global.SimpleSwordUnlocked},
            { "simple_bomb", Global.BombUnlocked },
            {"simple_knife", Global.SimpleKnifeUnlocked },
            { "simple_basket ball", Global.BasketBallUnlocked},
            {"simple_circle", Global.noRotateSwordUnlocked }
    };
       


    protected override void OnInit()
    {
        Items.Add(new UIGamePlanelItem(true).
         WithKey("simple_knife").
         WithDescription("升级暴击概率").
         WithName("升级暴击概率").
         WithPrice(Global.Swordlv.Value).
         WithDescription((item) =>
         {
             Global.BaoJI.Value += 0.1f;
         }));

        Items.Add(new UIGamePlanelItem(true).
         WithKey("simple_bomb").
         WithDescription("升级炸弹").
         WithName("升级炸弹").
         WithLv(Global.ExpAtklv.Value).
         WithDescription((item) =>
         {
             Global.BombUnlocked.Value = true;
         }));
         
        Items.Add(new UIGamePlanelItem(true).
         WithKey("Interval_upgrade").
         WithDescription("升级速度").
         WithName("升级速度").
         WithPrice(Global.Explv.Value).
         WithDescription((item) =>
         {
             Global.speed.Value++;

         }));

        Items.Add(new UIGamePlanelItem(true).
         WithKey("simple_sword").
         WithDescription("升级剑").
         WithName("升级剑").
         WithPrice(Global.Swordlv.Value).
         WithDescription((item) =>
         {
             Global.SimpleSwordUnlocked.Value = true;
             Global.Swordlv.Value++;
             AudioKit.PlaySound("coin");
             Time.timeScale = 1f;
             if (Global.Swordlv.Value % 2 == 0)
             {
                 Global.SimpleAbilityDamage.Value *= 2f;
                 Global.SimpleAbilityDuration.Value *= 0.7f;
                 Global.SimpleSwordCount.Value += 1;
             }
             else
             {
                 Global.SimpleAbilityDamage.Value *= 3f;
                 Global.SimpleAbilityDuration.Value *= 0.9f;
                 Global.SimpleSwordCount.Value += 1;
             }


         }));

        Items.Add(new UIGamePlanelItem(true).
         WithKey("simple_upgrade").
         WithDescription("升级匕首").
         WithName("升级匕首").
         WithPrice(Global.Swordlv.Value).
         WithDescription((item) =>
         {
             Global.SimpleKnifeUnlocked.Value = true;
             Global.Swordlv.Value++;
             AudioKit.PlaySound("coin");
             Time.timeScale = 1f;
             if (Global.Swordlv.Value % 2 == 0)
             {
                 Global.SimpleAbilityDamage.Value *= 2f;
                 Global.SimpleAbilityDuration.Value *= 0.7f;
                 Global.SimpleSwordCount.Value += 1;
                 Global.SimpleSwordSpeed.Value += 1;
             }
             else
             {
                 Global.SimpleAbilityDamage.Value *= 3f;
                 Global.SimpleAbilityDuration.Value *= 0.9f;
                 Global.SimpleSwordCount.Value += 1;
                 Global.SimpleSwordSpeed.Value += 1;
             }
         }));

        Items.Add(new UIGamePlanelItem(true).
         WithKey("simple_upgrade").
         WithDescription("升级锁").
         WithName("升级锁").
         WithPrice(Global.Swordlv.Value).
         WithDescription((item) =>
         {
             Global.RotateSwordUnlocked.Value = true;
             Global.Swordlv.Value++;
             AudioKit.PlaySound("coin");
             Time.timeScale = 1f;
             if (Global.Swordlv.Value % 2 == 0)
             {
                 Global.SimpleAbilityDamage.Value *= 2f;
                 Global.SimpleAbilityDuration.Value *= 0.7f;
                 Global.SimpleSwordCount.Value += 1;
             }
             else
             {
                 Global.SimpleAbilityDamage.Value *= 3f;
                 Global.SimpleAbilityDuration.Value *= 0.9f;
                 Global.SimpleSwordCount.Value += 1;
             }


         }));
        Items.Add(new UIGamePlanelItem(true).
         WithKey("rotate_sword").
         WithDescription("升级刀").
         WithName("升级刀").
         WithPrice(Global.Swordlv.Value).
         WithDescription((item) =>
         {
             Global.noRotateSwordUnlocked.Value = true;
             Global.Swordlv.Value++;
             AudioKit.PlaySound("coin");
             Time.timeScale = 1f;
             if (Global.Swordlv.Value % 2 == 0)
             {
                 Global.SimpleAbilityDamage.Value *= 2f;
                 Global.SimpleAbilityDuration.Value *= 0.7f;
                 Global.SimpleSwordCount.Value += 1;
             }
             else
             {
                 Global.SimpleAbilityDamage.Value *= 3f;
                 Global.SimpleAbilityDuration.Value *= 0.9f;
                 Global.SimpleSwordCount.Value += 1;
             }


         }));
        Items.Add(new UIGamePlanelItem(true).
         WithKey("basket_ball").
         WithDescription("升级篮球").
         WithName("升级篮球").
         WithPrice(Global.Swordlv.Value).
         WithDescription((item) =>
         {
             Global.BasketBallUnlocked.Value = true;
             Global.Swordlv.Value++;
             AudioKit.PlaySound("coin");
             Time.timeScale = 1f;
             if (Global.Swordlv.Value % 2 == 0)
             {
                 Global.SimpleAbilityDamage.Value *= 2f;
                 Global.SimpleAbilityDuration.Value *= 0.7f;
                 Global.SimpleSwordCount.Value += 1;
             }
             else
             {
                 Global.SimpleAbilityDamage.Value *= 3f;
                 Global.SimpleAbilityDuration.Value *= 0.9f;
                 Global.SimpleSwordCount.Value += 1;
             }


         }));
    
      OnExpUpgrdeSystemChanged.Register(() =>
      {
          Save();
          // var btn = this.GetSystem<ExpUpgrdeSystem>().TypeItems;
          // 	Global.type = Random.Range(0, btn.Count);
          // 		for (int i = 0; i < btn.Count; i++)
          // 		{
          //             if (btn[i] != null)
          //             {
          //                 if (Global.type == i)
          //                 {
          //                     btn[i].Show();
          //                 }
          //                 else
          //                 {
          //                     btn[i].Hide();
          //                 }
          //             }
          // 		}
      });
    }
    public void Say()
    {
       
    }

    public void Save()
    {
        var SaveSystem = this.GetSystem<SaveSystem>();
        foreach (var expUpgradeItem in Items)
        {
            SaveSystem.SaveBool(expUpgradeItem.Key, expUpgradeItem.UpgradeFinish);
            PlayerPrefs.SetInt(expUpgradeItem.Key, expUpgradeItem.UpgradeFinish ? 1 : 0);
        }
    }

    public void Roll()
    {

        foreach (var expUpgradeItem in Items)
        {
            expUpgradeItem.Visible = false;
        }
    }

    public void Load()
    {
        var SaveSystem = this.GetSystem<SaveSystem>();
        foreach (var expUpgradeItem in Items)
        {
            expUpgradeItem.UpgradeFinish = SaveSystem.LoadBool(expUpgradeItem.Key, false);
            foreach (var item in Items.Where(item => !item.UpgradeFinish).Take(2))
            {
                if (item == null)
                {

                }
                else
                {
                    item.Visible = true;
                }
            }


        }
    }
}
